package com.vf.editor.mvc.scene.plan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.ScenePolyline;
import com.vf.editor.mvc.scene.layer.SceneLayerService;

@Path(value = "/scene/plan", viewPath = "/views/scene/plan/")
public class ScenePlanController extends BaseController{

	@Inject
	private SceneLayerService sceneLayerService;
	
	/**
	 * 预案列表
	 */
	public void listPlan() {
		
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		
		String sqlExceptSelect = " from " + SceneLayer.TABLE_NAME + " o  where o.type='plan' ";
		
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " and o.name like '%" + keyword + "%'  ";
		}
		sqlExceptSelect += "  order by   o.name ";
		Page<SceneLayer> page = SceneLayer.dao.paginate(pageNumber, pageSize, "select o.* ", sqlExceptSelect);
		if(page.getList()!=null && page.getList().size()>0) {
			for (SceneLayer layer : page.getList()) {
				List<SceneLayer> children = new ArrayList<SceneLayer>();
				List<SceneLayer> cs = SceneLayer.dao.find("select * from "+SceneLayer.TABLE_NAME+" where parentId=? ",layer.getId());
				if(cs!=null && cs.size()>0) {
					for (SceneLayer c : cs) {
						SceneLayer e =sceneLayerService.getThing(c);
						children.add(e);
					}
				}
				layer.setChildren(children);
			}
		}
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
	/**
	 * 预案列表
	 */
	public void planTotal() {
		Map<String, Integer> res = new HashMap<String, Integer>();
		List<SceneLayer> lists = SceneLayer.dao.find("select * from "+SceneLayer.TABLE_NAME+" where type='plan' ");
		res.put("plan", lists==null?0:lists.size());
		renderSuccess("成功！", res);
	}
	
	public void getSubLine() {
		
		renderSuccess("",ScenePolyline.dao.findFirst("select a.type,b.* from scene_layer a,scene_polyline b where a.id=b.id and a.parentId=? and a.type='Polyline' LIMIT 1 ",getPara("id")));
		
	}
	
	public void library() {
		render("library.html");
	}
	
}
